/**
 * Copyright 厦门中软海晟信息技术有限公司 版权所有 违者必究 2019
 */
package com.leetCode;

/**
 * @author : sunzb(sunzb@hsit.com.cn)
 * @date: 2019/5/15
 */
public class _19_36_ValidSudoku {

    public boolean isValidSudoku(char[][] board) {
        boolean[] flags;
        int value;
        for (int row = 0; row < 9; row++) {
            flags = new boolean[10];
            for (int col = 0; col < 9; col++) {
                value = board[row][col] - '0';
                if (value > 0) {
                    if (flags[value])
                        return false;
                    flags[value] = true;
                }
            }
        }
        for (int col = 0; col < 9; col++) {
            flags = new boolean[10];
            for (int row = 0; row < 9; row++) {
                value = board[row][col] - '0';
                if (value > 0) {
                    if (flags[value])
                        return false;
                    flags[value] = true;
                }
            }
        }
        // 九宫格遍历
        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 3; col++) {
                int startRow = 3 * row;
                int startCol = 3 * col;
                flags = new boolean[10];
                for (int actRow = startRow; actRow < startRow + 3; actRow++) {
                    for (int actCol = startCol; actCol < startCol + 3; actCol++) {
                        value = board[actRow][actCol] - '0';
                        if (value > 0) {
                            if (flags[value])
                                return false;
                            flags[value] = true;
                        }
                    }
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        /*char[][] board = new char[][] {
                {'5','3','.','.','7','.','.','.','.'},
                {'6','.','.','1','9','5','.','.','.'},
                {'.','9','8','.','.','.','.','6','.'},
                {'8','.','.','.','6','.','.','.','3'},
                {'4','.','.','8','.','3','.','.','1'},
                {'7','.','.','.','2','.','.','.','6'},
                {'.','6','.','.','.','.','2','8','.'},
                {'.','.','.','4','1','9','.','.','5'},
                {'.','.','.','.','8','.','.','7','9'}
        };*/
        char[][] board = new char[][] {
                {'.','.','4','.','.','.','6','3','.'},
                {'.','.','.','.','.','.','.','.','.'},
                {'5','.','.','.','.','.','.','9','.'},
                {'.','.','.','5','6','.','.','.','.'},
                {'4','.','3','.','.','.','.','.','1'},
                {'.','.','.','7','.','.','.','.','.'},
                {'.','.','.','5','.','.','.','.','.'},
                {'.','.','.','.','.','.','.','.','.'},
                {'.','.','.','.','.','.','.','.','.'}};
        System.out.println(new _19_36_ValidSudoku().isValidSudoku(board));
    }
}
